package com.mall.web.control.app.user;
import java.util.Comparator;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.Scanner;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.mall.biz.user.AddressBizImpl;
import com.mall.client.user.AddressService;
import com.mall.client.user.LogisticsService;
import com.mall.pojo.Address;
import com.mall.pojo.Logistics;
import com.mall.pojo.User;
import com.mall.web.annotation.UserLoginMessage;
import com.mall.web.util.HttpUtil;
import com.mall.web.util.WebUtil;
import com.mall.web.vo.ResponseEntity;

@Controller
@RequestMapping("/user/address/")
public class AddressController {

	@Autowired
	private AddressService addressService;
	@Autowired
	private AddressBizImpl addressBiz;
	@Autowired
	private LogisticsService logisticsService;
	
	
	
//	@RequestMapping("test")
//	@ResponseBody
//	public Logistics test(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap) {
//		Logistics logisticsList = logisticsService.getLogisticsByLogisticsIdAndLogisticsCompany("3325374695980", "shentong");
//		return logisticsList;
//
//	}

	/**
	 * 地址列表
	 * 
	 * @param request
	 * @param response
	 * @param modelMap
	 * @return
	 */
	@UserLoginMessage
	@RequestMapping("addressList")
	public String addressList(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap) {
		User user = WebUtil.getCurrentUser(request);
		Integer onlyKey = HttpUtil.getCommkeyId(request, response);
		List<Address> addressList = this.addressBiz.getAddressListByUserAndOnlyKey(user, onlyKey, null);
		modelMap.put("addressList", addressList);
		return "user/address/addressList";

	}

	/**
	 * 地址详情
	 * 
	 * @param request
	 * @param response
	 * @param modelMap
	 * @return
	 */
	@UserLoginMessage
	@RequestMapping("addressInfo")
	public String addressInfo(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap) {
		User user = WebUtil.getCurrentUser(request);
		Long addressId = HttpUtil.getLongValueFromRequest(request, "addressId");
		Address address = this.addressService.getAddressById(addressId);
		modelMap.put("user", user);
		modelMap.put("address", address);
		return "user/address/addressInfo";

	}

	/**
	 * 新增地址页面
	 * 
	 * @param request
	 * @param response
	 * @param modelMap
	 * @return
	 */
	@UserLoginMessage
	@RequestMapping("addAddress")
	public String addAddress(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap) {
		User user = WebUtil.getCurrentUser(request);
		modelMap.put("user", user);
		return "user/address/addAddress";

	}

	/**
	 * 新增和修改地址接口
	 * 
	 * @param address
	 * @param request
	 * @param response
	 * @param modelMap
	 * @return
	 */
	@UserLoginMessage
	@RequestMapping("addAndModifyAddressAction")
	public ResponseEntity<Address> addAddressAction(Address address,HttpServletRequest request, HttpServletResponse response,
			ModelMap modelMap) {
		ResponseEntity<Address> responseEntity = new ResponseEntity<>();
		Integer type = HttpUtil.getIntegerValueFromRequest(request, "type");
		Integer provinceId = HttpUtil.getIntegerValueFromRequest(request, "provinceId");
		Integer cityId = HttpUtil.getIntegerValueFromRequest(request, "cityId");
		Integer areaId = HttpUtil.getIntegerValueFromRequest(request, "areaId");
		
		User user = WebUtil.getCurrentUser(request);
		Integer onlyKey = HttpUtil.getCommkeyId(request, response);
		Long modifyAddressId = HttpUtil.getLongValueFromRequest(request, "addressId");
		Boolean modifyAddress = false;
		Integer result = null;
		if (modifyAddressId != null) {
			address = this.addressService.getAddressById(modifyAddressId);
			if (address != null) {
				modifyAddress = true;
			}

		}
		if (type == 2) {//如果是修改或生成新的默认地址，则取消之前的默认地址
			List<Address> addressList = this.addressBiz.getAddressListByUserAndOnlyKey(user, onlyKey, 2);// 获取默认地址
			if (addressList != null) {
				Address addressMo = addressList.get(0);
				addressMo.setType(0);// 取消原来的默认地址
				addressMo.setGmtModify(new Date());
				result =this.addressService.modifyAddressByPrimaryKey(addressMo);
			}
		}
		address.setType(type);
		address.setUserId(user.getId());
		address.setStatus(0);
		address.setGmtModify(new Date());
	
		address.setCommOnlyKey(onlyKey);
		/*用户收获地址的省市区*/
		address.setProvinceId(provinceId);
		address.setCityId(cityId);
		address.setAreaId(areaId);
		
		if(modifyAddress){
			result = this.addressService.modifyAddressByPrimaryKey(address);
		}else{
			address.setGmtCreate(new Date());
			result = this.addressService.insertAddress(address);
		}
		if (result != null && result > 0) {
			responseEntity.initSuccResponseEntity(address, "add address succ");
		} else {
			responseEntity.initErrorResponseEntity(null, "add address succ");
		}
		return responseEntity;

	}
	/**
	 * 设置默认地址和取消默认地址
	 * @param request
	 * @param response
	 * @return
	 */
	@UserLoginMessage
	@RequestMapping("setDefultAddress")
	public ResponseEntity<Address> setDefultAddress(HttpServletRequest request, HttpServletResponse response) {
		ResponseEntity<Address> responseEntity = new ResponseEntity<>();
		Integer type = HttpUtil.getIntegerValueFromRequest(request, "type");//type = 2设置成默认地址；type == 取消设置成默认地址
		Long addressId = HttpUtil.getLongValueFromRequest(request, "addressId");
		Address address = this.addressService.getAddressById(addressId);
		User user = WebUtil.getCurrentUser(request);
		Integer onlyKey = HttpUtil.getCommkeyId(request, response);
		Integer result = null;
		Address addressMo =  null;
		if (type == 2) {//如果是修改或生成新的默认地址，则取消之前的默认地址
			List<Address> addressList = this.addressBiz.getAddressListByUserAndOnlyKey(user, onlyKey, 2);// 获取默认地址
			if (addressList != null) {
				addressMo = addressList.get(0);
				addressMo.setType(0);// 取消原来的默认地址
				addressMo.setGmtModify(new Date());
				result =this.addressService.modifyAddressByPrimaryKey(addressMo);
			}
		}
		address.setType(type);
		address.setGmtModify(new Date());
		result = this.addressService.modifyAddressByPrimaryKey(address);
		if (result != null && result > 0) {
			responseEntity.initSuccResponseEntity(addressMo, "add address succ");
		} else {
			responseEntity.initErrorResponseEntity(null, "add address succ");
		}
		return responseEntity;
	}
	public static void main(String[] args) {
		
		//System.out.println(HttpUtil.sendGet("http://m.kuaidi100.com/query?type=shentong&postid=3325374695980&id=1&valicode=&temp=", null));
	}

}
